package com.managermype.controller;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.managermype.business.GsonBusiness;
import com.managermype.business.UserBusiness;
import com.managermype.domain.Departament;
import com.managermype.domain.Enterprise;
import com.managermype.domain.Role;
import com.managermype.domain.UploadedFile;
import com.managermype.domain.User;
import com.managermype.exception.ManagerMypeException;
import com.managermype.util.Constant;
import com.managermype.util.Page;

@Controller
public class UserController extends BaseController {
	
	@Autowired
	private UploadedFile uploadedFile;

	@Autowired
	private User user;
	
	@Autowired
	private Departament departament;
	
	@Autowired
	private Role role;
	
	@Autowired
	private Enterprise enterprise;

	@Autowired
	private UserBusiness userBusiness;
	
	@Autowired
	private GsonBusiness gsonBusiness;
	
	@RequestMapping(value="/employees/principal.htm", method = RequestMethod.GET)
	public String principal(){
		return Page.EMPLOYEE_HOME_PAGE.getJspPath();
	}
	
	@RequestMapping(value="/employees/upload-cv.htm",method = RequestMethod.GET)
	public String uploadFile(){
		return "employee/upload-cv";
	}
	
	@RequestMapping(value="/employees/upload-cv.action",method = RequestMethod.POST)
	public @ResponseBody String uploadedFile(MultipartHttpServletRequest request){
		Iterator<String> itr = request.getFileNames();
		MultipartFile mpf = request.getFile(itr.next());
		String pathImage = servletContext.getRealPath("static/images/uploads");
		try{
			 File path = new File(pathImage);
			 
			 uploadedFile.length = mpf.getBytes().length;
			 uploadedFile.bytes= mpf.getBytes();
			 uploadedFile.type = mpf.getContentType();
			 uploadedFile.name = mpf.getOriginalFilename();
			 
			 mpf.transferTo(new File(path, uploadedFile.getName()));
			 return gsonBusiness.toJsonFormat(uploadedFile);
		}catch(Exception e){
			e.printStackTrace();
			return "ERROR";
		}
	}

	@RequestMapping(value = "/employees/add-user.htm", method = RequestMethod.GET)
	public String formAddUser(ModelMap modelMap) {
		return Page.ADD_USER_FORM_PAGE.getJspPath();
	}
	
	@RequestMapping(value="/employees/{username}.htm")
	public String profileEmployee(@PathVariable String username, ModelMap modelMap){
		try {
			user = userBusiness.getUserByUsername(username);
			modelMap.addAttribute("employee", user);
			return Page.PROFILE_EMPLOYEE.getJspPath();
		}
		catch (ManagerMypeException e) {
			modelMap.addAttribute(Constant.ERROR_MESSAGE, e.getMessage());
			return Page.PROFILE_EMPLOYEE.getJspPath();
		}
	}
	
	@RequestMapping(value = "/employees/add-user.action", method = RequestMethod.POST)
	public @ResponseBody String addUser(@RequestParam(value = "firstname", required = true) String firstname,
			@RequestParam(value = "lastname", required = true) String lastname,
			@RequestParam(value = "motherLastname", required = true) String motherLastname,
			@RequestParam(value = "birthdate", required = true) String birthdate,
			@RequestParam(value = "identity", required = true) String identity,
			@RequestParam(value = "gender", required = true) char gender,
			@RequestParam(value = "active", required = true) boolean active,
			@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "myRoles", required = true) Integer[] myIdRoles,
			@RequestParam(value = "myEnterprise", required = true) Integer idEnterprise,
			@RequestParam(value = "myDepartament", required = true) Integer idDepartament) {

		enterprise.setIdEnterprise(idEnterprise);
		departament.setIdDepartament(idDepartament);
		Set<Role> myRoles = new HashSet<Role>();

		for (Integer idRole : myIdRoles) {
			role.setIdRole(idRole);
			myRoles.add(role);
		}

		try {
			user = userBusiness.insertUser(username, password, firstname, lastname, motherLastname,
					toCalendar(birthdate), identity, gender, active, myRoles, enterprise, departament);
			return Constant.SUCCESS_AJAX_REQUEST;
		}
		catch (ManagerMypeException e) {
			return e.getMessage();
		}
		catch (Exception e) {
			return e.getMessage();
		}

	}

}
